package brainsplash.gyrusscope.game;

import javax.microedition.khronos.opengles.GL10;

import android.util.Log;
import brainsplash.gyrusscope.framework.gl.Animation;
import brainsplash.gyrusscope.framework.gl.Camera2D;
import brainsplash.gyrusscope.framework.gl.SpriteBatcher;
import brainsplash.gyrusscope.framework.gl.TextureRegion;
import brainsplash.gyrusscope.framework.impl.GLGraphics;

public class WorldRenderer {
    static final float FRUSTUM_WIDTH = 320;
    static final float FRUSTUM_HEIGHT = 480;    
    GLGraphics glGraphics;
    World world;
    Camera2D cam;
    SpriteBatcher batcher;
    float dir = 0;
    
    public WorldRenderer(GLGraphics glGraphics, SpriteBatcher batcher, World world) {
        this.glGraphics = glGraphics;
        this.world = world;
        this.cam = new Camera2D(glGraphics, FRUSTUM_WIDTH, FRUSTUM_HEIGHT);
        this.batcher = batcher;
    }
    
    public void render() {
        cam.setViewportAndMatrices();
        renderBackground();
        renderObjects();
    }
    
    public void renderBackground() {
        batcher.beginBatch(Assets.background);
        batcher.drawSprite(cam.position.x, cam.position.y,
                           FRUSTUM_WIDTH, FRUSTUM_HEIGHT, 
                           Assets.backgroundRegion);
        batcher.endBatch();
    }
    
    public void renderObjects() {
        GL10 gl = glGraphics.getGL();
        gl.glEnable(GL10.GL_BLEND);
        gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        
        batcher.beginBatch(Assets.items);
        renderParticles();
        renderSpaceships();
        renderAsteroids();
        renderBlasts();
        renderExplosions();
        batcher.endBatch();
        gl.glDisable(GL10.GL_BLEND);
    }

    private void renderParticles() {
        for (int j = 0; j < Particles.partSystems.size(); j ++) {
        	ParticleSystem ps = Particles.partSystems.get(j);
	        for(int i = 0; i < ps.particles.size(); i++) {
	        	BaseObject gobject = ps.particles.get(i);
	        	float offset = Math.signum(gobject.speed) * 90;
	        	if (offset == 0) {offset = 90;}
	            batcher.drawSprite(gobject.x, gobject.y, gobject.size, gobject.size, gobject.direction + offset, gobject.sprite);
	        }
        }
    }
    
    private void renderSpaceships() {
        for(int i = 0; i < world.spaceships.size(); i++) {
        	BaseObject gobject = world.spaceships.get(i);
        	//TODO: Consider animating sprites
            //TextureRegion keyFrame = Assets.humanship;
            //if(platform.state == Platform.PLATFORM_STATE_PULVERIZING) { //animations
            //    keyFrame = Assets.brakingPlatform.getKeyFrame(platform.stateTime, Animation.ANIMATION_NONLOOPING);
            //} 
        	float offset = Math.signum(gobject.speed) * 90;
        	if (offset == 0) {offset = 90;}
            batcher.drawSprite(gobject.x, gobject.y, gobject.size, gobject.size, gobject.direction + offset, gobject.sprite);
        }
        //Log.d(">>>>>>>", "Rendered | " + len);
    }
    
    private void renderBlasts() {
        for(int i = 0; i < world.blasts.size(); i++) {
        	BaseObject gobject = world.blasts.get(i);
        	float offset = Math.signum(gobject.speed) * 90;
        	if (offset == 0) {offset = 90;}
            batcher.drawSprite(gobject.x, gobject.y, gobject.size, gobject.size, gobject.direction + offset, gobject.sprite);
        }
    }
    
    private void renderAsteroids() {
        for(int i = 0; i < world.asteroids.size(); i++) {
        	BaseObject gobject = world.asteroids.get(i);
        	float offset = Math.signum(gobject.speed) * 90;
        	if (offset == 0) {offset = 90;}
            batcher.drawSprite(gobject.x, gobject.y, gobject.size, gobject.size, gobject.direction + offset, gobject.sprite);
        }
    }
    
    private void renderExplosions() {
        for(int i = 0; i < world.explosions.size(); i++) {
        	float extra = (5-world.explosions.get(i).duration);
        	BaseObject gobject = world.explosions.get(i);
            batcher.drawSprite(gobject.x, gobject.y, gobject.size+extra, gobject.size+extra, gobject.direction, gobject.sprite);
        }
    }

}
